﻿

var DIF = (function (dif) {
    var _masterFieldData = [],
        data = {
            xFormID: '',
            xFormName: '',
            xMasterTableName: '',
            xTemplateName: '',
            xMasterJson: '',
            xDetailsJson: '',
            xHtmlString: '',
            xVersion: '',
            xFieldItems: null,
            isNew: '0'
        };
    var _DetailsItems = null;
    //    var FieldItem = {
    //        ID: '',
    //        FormID: '',
    //        TableName: '',
    //        Caption: '',
    //        FieldName: '',
    //        DefualtValue: '',
    //        DataSourceType: '',
    //        DataSource: '',
    //        IsRequire: ''
    //    };

    function DetailsItem() {
        this.ID = "";
        this.DetailsTableName = "";
        this.PrimaryKey = "";

    };

    function FieldItem() {
        this.ID = '';
        this.FormID = '';
        this.TableName = '';
        this.Caption = '';
        this.FieldName = '';
        this.DefualtValue = '';
        this.DataSourceType = '';
        this.DataSource = '';
        this.IsRequire = '';
        this.ControlsType = '';
    };

    dif.setData = function (id, name, masterTableName, templateName, masterJson, detailsJson, htmlString, version, fieldItems, isNew, controlsType) {
        data.xFormID = id;
        data.xFormName = name;
        data.xMasterTableName = masterTableName;
        data.xTemplateName = templateName;
        data.xMasterJson = masterJson;
        data.xDetailsJson = detailsJson;
        data.xHtmlString = htmlString;
        data.xVersion = version;
        data.xFieldItems = fieldItems;
        data.isNew = isNew;
        data.ControlsType = controlsType;
    };
    dif.setxFormID = function (id) {
        data.xFormID = id;
    };
    dif.loadFormByID = function (id) {
        data.xFormID = id;
        data.isNew = '1';
    };
    dif.setMasterTableName = function (TableName) {
        data.xMasterTableName = TableName;
    };
    dif.getData = function () {
        return JSON.stringify(data);
    };
    dif.getFieldItems = function () {
        var _FieldItems = null;
        if (data) {
            _FieldItems = data.xFieldItems;
        }
        return _FieldItems
    };
    dif.getFieldItem = function (name) {
        var _FieldItem = null;
        if (data) {
            if (jlinq.from(data.xFieldItems).count() > 0) {
                _FieldItem = jlinq.from(data.xFieldItems).equals('FieldName', name).select();
                if (typeof (_FieldItem) != "undefined") {
                    if (typeof (_FieldItem[0]) == "undefined") { _FieldItem = null; }
                    else {
                        _FieldItem = _FieldItem[0];
                        if (typeof (_FieldItem.FieldName) == "undefined") { _FieldItem = null; }
                    }
                }
            }
        }
        return _FieldItem;
    };
    dif.fieldItemEmpty = function () {
        var item = new FieldItem();
        item.ID = '';
        item.FormID = '';
        item.TableName = '';
        item.Caption = '';
        item.FieldName = '';
        item.DefualtValue = '';
        item.DataSourceType = '';
        item.DataSource = '';
        item.IsRequire = '';
        item.ControlsType = '';
    };
    dif.AddFieldItem = function (id, formid, tablename, caption, fieldname, defualtvalue, datasourcetype, datasource, isrequire, controlsType) {
        var _Item = null;
        if (data) {

            var item = new FieldItem();
            item.ID = id;
            item.FormID = formid;
            item.TableName = tablename;
            item.Caption = caption;
            item.FieldName = fieldname;
            item.DefualtValue = defualtvalue;
            item.DataSourceType = datasourcetype;
            item.DataSource = datasource;
            item.IsRequire = isrequire;
            item.ControlsType = controlsType;

            data.xFieldItems.push(item);
        }
    };

    dif.AddDetailsNewItems = function (tableName, key) {
        var item = new DetailsItem();
        _DetailsItems = [];
        item.ID = NewGuid();
        item.DetailsTableName = tableName;
        item.PrimaryKey = key;

        _DetailsItems.push(item);

        DIF.getDetailsItemsJson();
    };
    dif.AddDetailsItems = function (id, tableName, key) {
        var item = new DetailsItem();
        item.ID = id;
        item.DetailsTableName = tableName;
        item.PrimaryKey = key;

        _DetailsItems.push(item);

        DIF.getDetailsItemsJson();
    };
    dif.getDetailsItemsJson = function () {
        var DetailsItemsJson = '';
        if (_DetailsItems.length > 0) {
            DetailsItemsJson = JSON.stringify(_DetailsItems);
        }
        data.xDetailsJson = DetailsItemsJson;
    }
    dif.checkFieldItem = function () {
        if (data) {
            if (_SelectArray.length > 0) {
                $.each(data.xFieldItems, function (n, value) {
                    var fieldName = value.FieldName;
                    if (typeof (fieldName) != "undefined") {
                        var isExist = $.inArray(fieldName, _SelectArray);

                        if (isExist < 0) {
                            var _items = data.xFieldItems.remove("FieldName", fieldName);
                            data.xFieldItems = _items;
                        }

                    }
                });
            }
            else { data.xFieldItems = []; }
        }
    }
    dif.init = function (id, name, masterTableName, templateName) {
        if (id == '' || id == 'nudefined') {
            data.xFormID = NewGuid();
            data.isNew = '0';
        }
        else {
            data.xFormID = id;
            data.isNew = '1';
        }
        data.xFormName = name;
        data.xMasterTableName = masterTableName;
        data.xTemplateName = templateName;
        data.xFieldItems = new Array();
        DIF.MasterData.init();
        _DetailsItems = new Array();
    };
    dif.setMasterJson = function (masterJson) {
        data.xMasterJson = masterJson;
    };
    dif.setDetailsJson = function (detailsJson) {
        data.xDetailsJson = detailsJson;
    };
    dif.setHtmlString = function (htmlString) {
        data.xHtmlString = htmlString;
    };
    dif.setHtmlString = function (htmlString) {
        data.xHtmlString = htmlString;
    };
    dif.getMasterJson = function () {
        var strJson = '';
        if (data) {
            strJson = data.xMasterJson;
        }
        return strJson;
    };
    dif.getDetailsJson = function () {
        var strJson = '';
        if (data) {
            strJson = data.xDetailsJson;
        }
        return strJson;
    };
    dif.getFormID = function () {
        var strFormID = '';
        if (data) {
            strFormID = data.xFormID;
            if (strFormID == '' || strFormID == 'nudefined') {
                strFormID = NewGuid();
                data.xFormID = strFormID;
            }
        }
        return strFormID;
    };
    dif.getHtmlString = function () {
        var strJson = '';
        if (data) {
            strJson = data.xHtmlString;
        }
        return strJson;
    };
    dif.getMasterTableName = function () {
        var tablename = '';
        if (data) {
            tablename = data.xMasterTableName;
        }
        return tablename;
    };
 
    return dif;
})(DIF || {});


DIF.MasterData = (function (masterdata) {
    var _baseAPIUrl = "TableData.aspx/GetMasterFieldList";
    var _currentParams = [];
    var _loaded = false;
    var _TableName = '';
    var _FieldData = [];
    masterdata.init = function () {
        _TableName = DIF.getMasterTableName();
        if (_TableName != _MasterTableName) {
            _loaded = false;
            _TableName = _MasterTableName;
        }
        DIF.MasterData.open();
    };
    //    masterdata.init = function (tablename) {
    //        _TableName = tablename;
    //        DIF.MasterData.open();
    //    };
    masterdata.getMasterFieldData = function () {
        return _FieldData;
    };
    masterdata.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            TableName: _TableName
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            formDatainit(data);
        }
    };
    function formDatainit(fieldList) {
        if (fieldList) {
            var person = JSON.parse(fieldList);
            //var strfieldList = JSON.stringify(fieldList);
            if (person) {
                _FieldData = setMasterData(person);
            }
        }
    };
    function setMasterData(o) {
        var r = new Array();
        if (o) {

            if (o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    dfield = [];
                    var item = o[i];
                    dfield.push(item.Notes);
                    dfield.push(item.FieldName);
                    r.push(dfield);
                }
            }
        }
        return r;
    };
    return masterdata
})(DIF.MasterData || {});

DIF.DetailsData = (function (detailsdata) {
    var _baseAPIUrl = "TableData.aspx/GetMasterFieldList";
    var _currentParams = [];
    var _loaded = false;
    var _TableName = '';
    var _DFieldData = [];
    detailsdata.init = function (tableName) {
        _TableName = tableName;
        if (_TableName != _DetailsTableName) {
            _loaded = false;
            _DetailsTableName = _TableName;
        }
        DIF.DetailsData.open();
    };

    detailsdata.getDetailsFieldData = function () {
        return _DFieldData;
    };

    detailsdata.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            TableName: _TableName
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            formDatainit(data);
        }
    };
    function formDatainit(fieldList) {
        if (fieldList) {
            var person = JSON.parse(fieldList);
            //var strfieldList = JSON.stringify(fieldList);
            if (person) {
                _DFieldData = setDetailsData(person);
            }
        }
    };
    function setDetailsData(o) {
        var r = new Array();
        if (o) {

            if (o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    dfield = [];
                    var item = o[i];
                    dfield.push(item.Notes);
                    dfield.push(item.FieldName);
                    r.push(dfield);
                }
            }
        }
        return r;
    };
    return detailsdata
})(DIF.DetailsData || {});


DIF.DetailsItems = (function (detailsitems) {
    var _baseAPIUrl = "";
    var _currentParams = [];
    var _loaded = false;


    detailsitems.init = function (detailsJson) {
        if (detailsJson) {
            var person = JSON.parse(detailsJson);
            if (person) {
                if (person.length > 0) {
                    for (var i = 0; i < person.length; i++) {
                        var detailsitem = person[i];
                        var _ID = detailsitem.ID;
                        var _TableName = detailsitem.DetailsTableName;
                        
                        var _PrimaryKey = detailsitem.PrimaryKey;
                        DIF.DetailsData.init(_TableName);
                        //DIF.setDetailsJson(detailsJson);
                        DIF.AddDetailsItems(_ID, _TableName, _PrimaryKey);

                    }
                }
            }
        }
    };
    return detailsitems
})(DIF.DetailsItems || {});

DIF.CodeSmithExplorer = (function (codesmithexplorer) {
    var _baseAPIUrl = 'TableData.aspx/GetCodeSmithCompleteString';
    var _currentParams = [];
    var _loaded = false;
    var _tablename = '';
    var _formName = '';
    var _masterTableName = '';
    var _strJson = '';
    var _formdata = '';
    var _colnum = '';
    var _formType = '';
    codesmithexplorer.init = function (formName, masterTableName, strJson, colnum, formType) {
        _formName = formName;
        _masterTableName = masterTableName;
        _strJson = strJson;
        _colnum = colnum;
        _formType = formType;
        DIF.CodeSmithExplorer.open();
    }; //(string formName, string masterTableName, string strJson)
    codesmithexplorer.getFormString = function () {
        return _formdata;
    };
    codesmithexplorer.open = function () {
        //        if (!_loaded) {
        //            
        //            _loaded = true
        //        }
        _load();
    };
    function _load(append) {
        _currentParams = {
            formName: _formName,
            masterTableName: _masterTableName,
            strJson: _strJson,
            colnum: _colnum,
            FormType: _formType
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    };
    function _populate(data, append) {
        if (data) {
            _formdata = data;

            if (oCKeditor != null) {
                if (_formType == "Master") {
                    oCKeditor.setData(data);
                } else {
                    if (oCKeditor.mode == 'wysiwyg') {
                        oCKeditor.insertHtml(data);
                    }
                    else
                        alert('You must be in WYSIWYG mode!');
                 }

            }
        }
    };

    return codesmithexplorer
})(DIF.CodeSmithExplorer || {});

DIF.GetFormFieldItems = (function (getFormFieldItems) {
    var _baseAPIUrl = 'TableData.aspx/getFormFieldItems';
    var _currentParams = [];
    var _loaded = false;
    var _tablename = '';
    var _formID = '';

    var _dataArray = null;

    getFormFieldItems.init = function (formID) {
        _formID = formID;
        _loaded = false;
        DIF.GetFormFieldItems.open();
    }; //(string formName, string masterTableName, string strJson)

    getFormFieldItems.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            formID: _formID
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    };
    function _populate(data, append) {
        if (data) {
            var person = JSON.parse(data);
            //var strfieldList = JSON.stringify(fieldList);
            if (person) 
            {
                if (person.length > 0) {
                    _SelectArray = [];
                    for (var i = 0; i < person.length; i++) {

                        var fielditem = person[i];
                        var _fieldName = fielditem.FieldName
                        _SelectArray.push(fielditem.FieldName);


                        var item = DIF.getFieldItem(_fieldName);
                        if (!item) {
                            //ID, FormID, TableName, Caption, FieldName, DefualtValue, DataSourceType, DataSource, IsRequire
                            //id, formid, tablename, caption, fieldname, defualtvalue, datasourcetype, datasource, isrequire
                            DIF.AddFieldItem(
                                            fielditem.ID,
                                            fielditem.FormID,
                                            fielditem.TableName,
                                            fielditem.Caption,
                                            fielditem.FieldName,
                                            fielditem.DefualtValue,
                                            fielditem.DataSourceType,
                                            fielditem.DataSource,
                                            fielditem.IsRequire,
                                            fielditem.ControlsType
                                            );
                        }
                    }
                }
            }


        }
    };

    return getFormFieldItems
})(DIF.GetFormFieldItems || {});

DIF.GetIExistFormName = (function (getIExistFormName) {
    var _baseAPIUrl = 'TableData.aspx/GetIExistFormName';
    var _currentParams = [];
    var _loaded = false;
    var _formname = '';


    getIExistFormName.init = function (formname) {
        _formname = formname;
        _loaded = false;
        DIF.GetIExistFormName.open();
    };

    getIExistFormName.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            formName: _formname
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    };
    function _populate(data, append) {
        if (data) {

            if (data == "1") {
                alert("您输入表单名已经在，请重新输入！"); 
            }

        }
    };

    return getIExistFormName
})(DIF.GetIExistFormName || {});

DIF.IsTableExists = (function (isTableExists) {
    var _baseAPIUrl = 'TableData.aspx/IsTableExists';
    var _currentParams = [];
    var _loaded = false;
    var _tablename = '';
    var _isExists = '0';


    isTableExists.init = function (Talbename) {
        _tablename = Talbename;
        _loaded = false;
        DIF.IsTableExists.open();
    };

    isTableExists.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };
    function _load(append) {
        _currentParams = {
            TableName: _tablename
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    };
    function _populate(data, append) {
        if (data) {
            if (data == "1") {
                _isExists = '1';
            }
        }
    };
    isTableExists.IsExists = function () {
        return _isExists;
    };
    return isTableExists
})(DIF.IsTableExists || {});